<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getReportDetailApi, getWeekReportApi } from '@/services/report'
import { useReportStore } from '@/stores'
const reportStore = useReportStore()
const endDate = ref('')
const nowdate = ref('')
const props = defineProps<{
  deviceNum: string
  type: string
}>()
const yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000)
const handledate = (date: any) => {
  const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  return date.getFullYear() + '-' + month + '-' + date.getDate()
}
//设置报告日期
const change = async (e: any) => {
  console.log(e.year + '-' + e.month + '-' + e.date)
  if (props.type == 'weekly') {
    const weekres = await getWeekReportApi({
      date: e.year + '-' + e.month + '-' + e.date,
      mtrlId: props.deviceNum,
    })
    if (!weekres.data) {
      uni.showToast({
        title: '该日期没有报告',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    reportStore.setWeekReport(weekres.data)
    nowdate.value = e.year + '-' + e.month + '-' + e.date
    uni.navigateTo({
      url: `/pages/report-weekly/report-weekly?deviceNum=${props.deviceNum}`,
    })
  } else {
    const res = await getReportDetailApi({
      date: e.year + '-' + e.month + '-' + e.date,
      mtrlId: props.deviceNum,
    })
    if (!res.data) {
      uni.showToast({
        title: '该日期没有报告',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    reportStore.setReport(res.data)
    nowdate.value = e.year + '-' + e.month + '-' + e.date
    uni.navigateTo({
      url: `/pages/report-daily/report-daily?deviceNum=${props.deviceNum}`,
    })
  }

  //console.log(nowdate.value)
}
onLoad(() => {
  console.log(props.deviceNum)
  nowdate.value = handledate(yesterday)
  endDate.value = nowdate.value
})
</script>

<template>
  <view class="calendar">
    <uni-calendar :lunar="true" :end-date="endDate" @change="change" :date="nowdate" />
    <view class="tips" v-if="props.type == 'weekly'">周报日期范围为当前选定的日期的前一周</view>
  </view>
</template>

<style lang="scss">
page {
  //修改今天的日期的数字和汉字颜色
  .uni-calendar-item--isDay-text {
    color: #c0c0c0 !important  ;
  }
  .uni-calendar-item__weeks-lunar-text {
    color: #c0c0c0 !important   ;
  }
  //不显示返回今天键
  .uni-calendar__backtoday {
    display: none;
  }
  .tips {
    margin: 38.462rpx auto;
    width: 423.077rpx;
    color: #c0c0c0;
    font-size: 23.077rpx;
  }
}
</style>
